<!DOCTYPE html>
<html>
  <head>
    <title>好单线报</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/minireset.css/0.0.2/minireset.css">
    <link rel="stylesheet" href="https://at.alicdn.com/t/font_1534407_cqy0t98abrg.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/vant@2.12/lib/index.css">
    <script src="https://cdn.bootcdn.net/ajax/libs/promise-polyfill/8.2.0/polyfill.min.js"></script>
    <script src="https://cdn.staticfile.org/jquery/2.2.4/jquery.min.js"></script>
    <script src="https://cdn.staticfile.org/vue/2.6.10/vue.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vant@2.12/lib/vant.min.js"></script><!--pc端click代替touch-->
    <script src="https://cdn.jsdelivr.net/npm/@vant/touch-emulator"></script>
    <meta name="referrer" content="no-referrer">
    <script defer>document.write('<link rel="stylesheet" href="https://img.bc.haodanku.com/cms/rt_xb.css?t=' + (Date.now() - Date.now() % (60 * 1000)) + '"/>')</script>
    <script src="https://img.bc.haodanku.com/cms/utils.v1_0_5.js"></script>
  </head>
  <body>
    
    <template id="c-list-item">
  <div class="goods-card">
    <div class="type-title">
      <img
        v-if="item.source == 2"
        class="jd"
        src="http://img-haodanku-com.cdn.fudaiapp.com/Fi4ijtJvI0QJWqmMpwI2sWQ5gOJn"
        alt=""
      >
      <img
        v-else-if="item.source == 1"
        class="tb"
        src="http://img-haodanku-com.cdn.fudaiapp.com/Fgj3o1aNeDaQe0BRrAqXSDx1pyz0"
        alt=""
      >
      <span class="text">{{item.label}}</span><span class="time">{{item.create_time}}</span>
    </div>
    <div class="detail-block">
      <div class="row" v-for="(item,index) in list">
        <div class="left" ref="copyBase">
          <ul>
            <li v-for="it in item.content">
              <div v-if="it.type === 'name'" class="title">{{it.value+"\n"}}</div>
              <template v-else>
                <button class="btn buy" v-on:click="buy($event,it)">马上抢</button>
                <div class="transfer"><br>{{it.transfer}}</div>
                <!--              <div style="word-break: break-all;">{{it.transfer}}</div>-->
              </template>
            </li>
          </ul>
        </div>
        <div class="right" v-if="item.image">
          <img v-on:click="previewImg(index)" referrerpolicy="no-referrer" v-bind:src="item.image" alt="">
        </div>
      </div>
    </div>
    <div class="btn-block">
      <button class="btn copy-text" v-on:click="copy">复制文案</button>
    </div>
  </div>
</template>
<script>
var labels = ["₳", "$", "¢", "₴", "€", "₤", "￥", "＄", "《", "\\(", "\/"];
var labelLeft = "[" + labels.join("") + "]";
var taoCodeReg = new RegExp(labelLeft + "\\w{11}");
var urlReg = new RegExp("^(?!mailto:)(?:(?:http|https|ftp)://|//)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-*)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-*)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$", "i");
var urlMatchReg = getFormatStr("(%s)", urlReg.source.substring(1, urlReg.source.length - 1));
urlMatchReg = new RegExp(urlMatchReg);

function getQueryString(name) {  
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); 
      
    var r = window.location.search.substr(1).match(reg);  
    if (r != null) {  
        return unescape(r[2]);  
    }  
    return null;  
}  

var yid = getQueryString("yid")


Vue.component("list-item", {
  template: "#c-list-item",
  props: {
    item: Object,
    type: {
      type: String,
      default: "tb" // tb || jd
    }
  },

  data: function() {
    return {
      list: []
    };
  },
  computed: {},
  watch: {
    item: {
      immediate: true,
      deep: true,
      handler: function(n) {
        if(!n) return;
        var arr;
        if(Array.isArray(n.step) && n.step.length) {
          arr = n.step;
        } else {
          arr = [{content: n.content, image: n.image}];
        }
        var that = this;
        this.list = arr.map(function(item) {
          /**
           * @type {{value:string, transfer:string, type:'url'|'code'|'name'}[]}
           */
          var content = [];
          item.content.split(/(?:\\n)+/).forEach(function(v) {
            v = v.trim();
            var result = {value: v, type: "name", transfer: ""};
            content.push(result);
            if(urlReg.test(v)) {
              result.type = "url";
            } else if(taoCodeReg.test(v)) {
              result.type = "code";
            } else if(urlMatchReg.test(v)) {
              var $1 = RegExp.$1;
              result.value = v.replace($1, "");
              content.push({value: $1, type: "url", transfer: ""});
            }
          });
          return {content: content, image: that.getImage(item.image)};
        });
      }

    }

  },
  methods: {
    getImage: function(img) {
      // var img = this.item.image;
      if(!img) return "";
      return img.replace(/https?:/, "");
    },
    isJdLink: function(value) {
      return /accompany\.m\.jd\.com/.test(value);
    },
    previewImg: function(index) {
      vant.ImagePreview({
        images: this.list.map(function(it) {
          return it.image;
        }),
        startPosition: index || 0
      });
    },
    buy: function(e, listItem) {
      if(this.isJdLink(listItem.value)) {
        window.top.location = listItem.value;
      } else {
        this.$parent.handleGoodsTransfer(e, this.item, listItem, "copy");
      }
    },
    copy: function(e) {
      var that = this;

      var list = this.list;
      var parent = that.$parent;
      var strArr = list.map(function(it) {
        return it.content.map(function(item) {
          if(item.type === "name" || item.type === "url" && that.isJdLink(item.value)) {
            return item.value;
          } else {
            if(!item.transfer) {
              parent.handleGoodsTransfer(e, that.item, item, "get");
            }
            return item.transfer;
          }
        }).join("\n");
      });
      // vue渲染是异步，会导致复制失败，js直接生成
      copy(strArr.join("\n\n"), "文案复制成功", "复制失败,请点击重试", true);
    }
  }
});
</script>

    <script src="https://cdn.bootcdn.net/ajax/libs/vant/2.12.13/vant.min.js"></script>
    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/vant/2.12.13/index.min.css">
    <div class="page-container" id="single-page-app" v-cloak>
      <div class="banner-wrapper"><img class="free-banner" v-bind:src="banner" alt=""></div>
      <div class="page-content" ref="list" v-bind:class="{'empty-list':!listData.list.length}">
        <ul class="nav">
          <li class="btn" v-for="(item, index) in nav.list" v-bind:class="{active: index === nav.activeIndex}" v-on:click="onNavChange(index)">{{ item.type_name }}</li>
        </ul>
        <div class="lists">
          <template v-if="listData.list.length || listData.finished">
            <list-item class="free-item" v-for="(item, index) in listData.list" v-bind:item="item" v-bind:key="index" v-on:buy="handleGoodsTransfer($event,item,'link')" v-on:share="handleGoodsTransfer($event,item,'copy')"></list-item><!-- 底部提示 -->
            <div class="list-item-loading"><span v-if="listData.finished">没有更多商品了~</span><span v-else>数据正在赶来的路上...</span></div>
          </template><!-- 加载效果 -->
        </div>
      </div>
      <div class="btn back-top" v-show="scrollTop &gt; 1000" v-on:click="backTop"></div>
      
      <van-overlay v-bind:show="loading">
        <van-loading color="#0094ff"></van-loading>
      </van-overlay>
    </div>
    <script>
      Vue.use(vant.Lazyload, {
  preLoad: 1.3,
  error: "http://img.bc.haodanku.com/haodanku/1595237918",
  loading: "http://img.bc.fqapps.com/fudai13cae4ae6ef16739ed3b100a2ec39e97.gif",
  attempt: 1
});

var listDataStr = JSON.stringify({
  list: [],
  loading: false,
  finished: false,
  min_id: 1,
  pageTotal: 1,
  pageCurrent: 0
});


// 根据微信浏览器缩放比例设置rem缩放
if(inWechat() && "ontouchstart" in window) {
  var scale = getFontScale(true);
  document.documentElement.style.fontSize = "calc(100vw / 7.5 * " + scale + ")";
}


var baseMixin = {
  el: "#single-page-app",
  data: function() {
    return {
      listData: JSON.parse(listDataStr),
      scrollTop: 0
    };
  },
  created: function() {
    this.getListData();
  },
  mounted: function() {
    var that = this;
    window.addEventListener("scroll", function(ev) {
      that.scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
      if(isScrollEnd(this, "vertical", 40)) {
        that.onScrollEnd();
      }
    });
  },
  methods: {
    onScrollEnd: function() {
      this.getListData();
    },
    resetListData: function() {
      this.listData = JSON.parse(listDataStr);
    },
    getListDataParams: function(item) {
      throw new Error("需要使用了该mixins的组件实现listDataParams函数");
    },
    getListData: function() {
      throw new Error("需要使用了该mixins的组件实现getListData函数");
    },
    getTransferParams: function() {
      throw new Error("需要使用了该mixins的组件实现transferParams函数");
    },
    handleGoodsTransfer: function() {
      throw new Error("需要使用了该mixins的组件实现handleGoodsTransfer函数");
    }
  }
};var indexMixin = (function() {
  var that;
  baseMixin.created = undefined;
  return {
    mixins: [baseMixin],
    directives: {
      sticky: sticky
    },
    data: function() {
      return {
        nav: {
          list: [],
          activeIndex: 0
        },
        banner: "http://img-haodanku-com.cdn.fudaiapp.com/FhRoRudSKzdJb902U9JPKQ693Qxz",
        loading: false,
        transferUrl: "https://tkf.ink/wire_report_ratesurl/"// 转链链接
      };
    },
    mounted: function() {
      that = this;
      this.getListData(true);
    },
    computed: {
      activeNav: function() {
        return this.nav.list[this.nav.activeIndex];
      }
    },
    methods: {
      scrollToList: function() {
        var top = this.$refs.list.offsetTop;
        var searchBoxHeight = this.$refs.searchBox.offsetHeight;
        window.scrollTo(0, top - searchBoxHeight);
      },
      onNavChange: function(index) {
        if(this.listData.loading || this.nav.activeIndex === index) return;
        this.nav.activeIndex = index;
        this.reGetListData();
      },
      resetListData: function() {
        this.listData = {
          list: this.listData.list,
          loading: false,
          finished: false,
          min_id: 1
        };
      },
      reGetListData: function() {
        this.resetListData();
        return this.getListData(true);
      },
      /**
       * 获取商品数据
       * @param {boolean} [clean = false]
       */
      getListData: function(clean) {
        if(this.loading || this.listData.loading || this.listData.finished) return;
        this.listData.loading = true;
        if(clean) {
          this.loading = true;
        }
        var params = Object.assign(
          {
            type: (this.activeNav || {type_id: "0"}).type_id, //	类型：0全部、1京东实时线报、2券优惠、3天猫凑单、4天猫超市、5整点抢购、6淘宝实时线报
            page_id: this.listData.min_id, //	选填	页码
            min_size: 10 //	选填	页数，最大单页100条
          },
          this.getListDataParams()
        );

        var obj = Request.get(url("v2.api.haodanku.com/wire_report"), params);
        // this.getListDataAbort = obj.ajax.abort.bind(obj.ajax);
        obj.promise.then(function(res) {
          var navList = res.label || [];
          navList.sort(function(a, b) {
            return a.type_name.length - b.type_name.length;
          });
          that.nav.list = navList;
          var listData = res.data || [];
          setTimeout(function() {
            if(clean) {
              that.listData.list = listData;
            } else {
              that.listData.list = that.listData.list.concat(listData);
            }
            var min_id = res.page_id;
            that.listData.finished = that.listData.min_id == min_id || !listData.length;
            that.listData.min_id = min_id || that.listData.min_id;
            // 微信浏览器 promise没有finally
            that.listData.loading = false;
            that.loading = false;
          }, 200);
        }).catch(function(e) {
          console.error(e);
          if(clean) {
            that.listData.list = [];
          }
          that.listData.finished = true;
        }).finally(function() {
          setTimeout(function() {
            that.listData.loading = false;
            that.loading = false;
          }, 250);
        });
        return obj.promise;
      },
      /**
       * 商品转链
       * @param e {MouseEvent}
       * @param goodsItem
       * @param {object} listItem
       * @param {string} listItem.value
       * @param {string} listItem.transfer
       * @param {'name'|'code'|'url'} listItem.type
       * @param {'get'|'copy'} type
       */
      handleGoodsTransfer: function(e, goodsItem, listItem, type) {
        var el = e.currentTarget.parentNode;

        var exec = function() {
          if(!listItem.transfer) return;
          var copyText = listItem.transfer;
          if(!urlReg.test(copyText)) {
            // 换行
            var div = document.createElement("div");
            div.innerHTML = copyText;
            Object.assign(div.style, {
              position: "fixed",
              left: "-10000px",
              top: "-10000px",
              whiteSpace: "pre-line"
            });
            document.body.appendChild(div);

            var rmDiv = function() {
              document.body.removeChild(div);
            };
            var successMsg = taoCodeReg.test(copyText) ? "复制淘口令成功，请打开淘宝领取" : "复制成功!";
            copy(div, successMsg, true).then(rmDiv, rmDiv);
          } else {
            window.top.location = listItem.transfer;
          }
        };

        if(listItem.transfer && type === "copy") {
          exec();
          return;
        }
        if(type === "copy") {
          el.addEventListener("click", function() {
            exec();
            el.removeEventListener("click", arguments.callee, false);
          }, false);
        }
        var params = Object.assign({
          content: listItem.value,
          type: goodsItem.source,
          wire_id: goodsItem.wire_id,
          version: "1.0.1"
        }, this.getTransferParams(goodsItem));
        Request.postSync(this.transferUrl, params, function(res) {
          listItem.transfer = res.data.data;
          // exec();
        }.bind(this)).promise.catch(function(res) {
          toast(res.msg || "转链失败");
        });
      },
      backTop: function() {
        $("html,body").animate({scrollTop: 0});
      }
    }
  };
})();
    </script>
    <script>
      new Vue({
        mixins: [indexMixin],
        methods: {
          // 列表参数
          getListDataParams: function(item) {
            return {
              apikey: "zqapi"//必填
            };
          },
          // 转链参数
          getTransferParams: function(item) {
            return {
              yid: yid
            };
          }
        }
      })
    </script>
  </body>
</html>